package com.dubbo.lian.common.util;

import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;

import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * TimeUtil
 * 
 */
public class TimeUtil {
	private final static long minute = 60 * 1000;// 1分钟
	private final static long hour = 60 * minute;// 1小时
	private final static long day = 24 * hour;// 1天
	private final static long month = 31 * day;// 月
	private final static long year = 12 * month;// 年

	private static String[] parsePatterns = { "yyyy-MM-dd",
			"yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM", "yyyy/MM/dd",
			"yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM", "yyyy.MM.dd",
			"yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM" };

	private TimeUtil() {
		throw new AssertionError();
	}



	
	public static String getTimeFormatText(Date date) {
		if (date == null) {
			return null;
		}
		long diff = new Date().getTime() - date.getTime();
		long r = 0;
		if (diff > year) {
			r = (diff / year);
			return r + "年";
		}
		if (diff > month) {
			r = (diff / month);
			return r + "个月";
		}
		if (diff > day) {
			r = (diff / day);
			return r + "天";
		}
		if (diff > hour) {
			r = (diff / hour);
			return r + "小时";
		}
		if (diff > minute) {
			r = (diff / minute);
			return r + "分钟";
		}
		return "刚刚";
	}

	/**
	 * 得到当前日期字符串 格式（yyyy-MM-dd）
	 */
	public static String getDate() {
		return getDate("yyyy-MM-dd");
	}

	/**
	 * 得到当前日期字符串 格式（yyyy-MM-dd） pattern可以为："yyyy-MM-dd" "HH:mm:ss" "E"
	 */
	public static String getDate(String pattern) {
		return DateFormatUtils.format(new Date(), pattern);
	}

	/**
	 * 得到日期字符串 默认格式（yyyy-MM-dd） pattern可以为："yyyy-MM-dd" "HH:mm:ss" "E"
	 */
	public static String formatDate(Date date, Object... pattern) {
		String formatDate = null;
		if (pattern != null && pattern.length > 0) {
			formatDate = DateFormatUtils.format(date, pattern[0].toString());
		} else {
			formatDate = DateFormatUtils.format(date, "yyyy-MM-dd");
		}
		return formatDate;
	}

	/**
	 * 得到日期时间字符串，转换格式（yyyy-MM-dd HH:mm:ss）
	 */
	public static String formatDateTime(Date date) {
		return formatDate(date, "yyyy-MM-dd HH:mm:ss");
	}

	/**
	 * 得到当前时间字符串 格式（HH:mm:ss）
	 */
	public static String getTime() {
		return formatDate(new Date(), "HH:mm:ss");
	}

	/**
	 * 得到当前日期和时间字符串 格式（yyyy-MM-dd HH:mm:ss）
	 */
	public static String getDateTime() {
		return formatDate(new Date(), "yyyy-MM-dd HH:mm:ss");
	}
	public static String getDateHHMMSS() {
		return formatDate(new Date(), "HHmmss");
	}
	/**
	 * 得到当前日期和时间字符串 格式（yyyy-MM-dd HH:mm:ss）
	 */
	public static String getTimeYYMMDD() {
		return formatDate(new Date(), "yyyy-MM-dd");
	}
	/**
	 * 得到当前日期和时间字符串 格式（yyyy-MM-dd HH:mm:ss）
	 */
	public static String getDateTimes() {
		return formatDate(new Date(), "yyyy-MM-dd HH:mm");
	}

	/**
	 * 得到当前年份字符串 格式（yyyy）
	 */
	public static String getYear() {
		return formatDate(new Date(), "yyyy");
	}

	/**
	 * 得到当前月份字符串 格式（MM）
	 */
	public static String getMonth() {
		return formatDate(new Date(), "MM");
	}

	/**
	 * 得到当天字符串 格式（dd）
	 */
	public static String getDay() {
		return formatDate(new Date(), "dd");
	}
	/**
	 *
	 */
	public static int getWeekDay(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		return cal.get(Calendar.DAY_OF_WEEK);
	}
	/**
	 * 得到当前星期字符串 格式（E）星期几
	 */
	public static String getWeek() {
		return formatDate(new Date(), "E");
	}

	/**
	 * 日期型字符转化为日期 格式 { "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm",
	 * "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy.MM.dd",
	 * "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm串" }
	 */
	public static Date parseDate(Object str) {
		if (str == null) {
			return null;
		}
		try {
			return org.apache.commons.lang.time.DateUtils.parseDate(
					str.toString(), parsePatterns);
		} catch (ParseException e) {
			return null;
		}
	}

	/**
	 * 获取过去的天数
	 *
	 * @param date
	 * @return
	 */
	public static long pastDays(Date date) {
		long t = new Date().getTime() - date.getTime();
		return t / (24 * 60 * 60 * 1000);
	}

	/**
	 * 获取过去的小时
	 *
	 * @param date
	 * @return
	 */
	public static long pastHour(Date date) {
		long t = new Date().getTime() - date.getTime();
		return t / (60 * 60 * 1000);
	}

	/**
	 * 获取过去的分钟
	 *
	 * @param date
	 * @return
	 */
	public static long pastMinutes(Date date) {
		long t = new Date().getTime() - date.getTime();
		return t / (60 * 1000);
	}

	/**
	 * 转换为时间（天,时:分:秒.毫秒）
	 *
	 * @param timeMillis
	 * @return
	 */
	public static String formatDateTime(long timeMillis) {
		long day = timeMillis / (24 * 60 * 60 * 1000);
		long hour = (timeMillis / (60 * 60 * 1000) - day * 24);
		long min = ((timeMillis / (60 * 1000)) - day * 24 * 60 - hour * 60);
		long s = (timeMillis / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
		long sss = (timeMillis - day * 24 * 60 * 60 * 1000 - hour * 60 * 60
				* 1000 - min * 60 * 1000 - s * 1000);
		return (day > 0 ? day + "," : "") + hour + ":" + min + ":" + s + "."
				+ sss;
	}

	/**
	 * 获取两个日期之间的天数
	 *
	 * @param before
	 * @param after
	 * @return
	 */
	public static int getDistanceOfTwoDate(Date before, Date after) {
		long beforeTime = before.getTime();
		long afterTime = after.getTime();
		return (int)(afterTime - beforeTime) / (1000 * 60 * 60 * 24);
	}


	public static String format(Date date) {
		try {
			return DateFormatUtils.format(date, "yyyy-MM-dd HH:mm:ss");
		} catch (Exception e) {
			return null;
		}
	}

	public static String format(Date date,String pattern) {
		try {
			return DateFormatUtils.format(date, pattern);
		} catch (Exception e) {
			return null;
		}
	}

	/**
	 * 日期累加
	 *
	 * @param format
	 *            返回的日期格式
	 * @param StrDate
	 *            要累加的日期
	 * @param year
	 *            加多少年
	 * @param month
	 *            加多少个月
	 * @param day
	 *            加多少天
	 * @return
	 */
	public static String GetSysDate(String format, String StrDate, int year,
									int month, int day) {
		Calendar cal = Calendar.getInstance();
		SimpleDateFormat sFmt = new SimpleDateFormat(format);
		cal.setTime(sFmt.parse((StrDate), new ParsePosition(0)));

		if (day != 0) {
			cal.add(cal.DATE, day);
		}
		if (month != 0) {
			cal.add(cal.MONTH, month);
		}
		if (year != 0) {
			cal.add(cal.YEAR, year);
		}
		return sFmt.format(cal.getTime());
	}

	public static long getInterval(String begin, String end) {
		try {
			Date beginTime = DateUtils.parseDate(begin, "yyyy-MM-dd");
			Date endTime = DateUtils.parseDate(end, "yyyy-MM-dd");
			long day = 0;
			day = (beginTime.getTime() - endTime.getTime()) / (24 * 60 * 60 * 1000);
			return Math.abs(day);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public static String getDelay(String now, Integer delay, String pattern, Integer time) {
		String delayDate = null;
		try {
			Date date = parse(now, pattern);
			long delayTime = (date.getTime() / 1000) + delay * time;
			date.setTime(delayTime * 1000);
			delayDate = DateFormatUtils.format(date, pattern);

			return delayDate;
		} catch (Exception e) {
			return null;
		}
	}

	public static String getPreMonthFirst(String now, Integer delay, String pattern) {
		Calendar c = Calendar.getInstance();
		c.setTime(parse(now, pattern));
		c.set(Calendar.DATE, 1);
		c.add(Calendar.MONTH, -1);

		return DateFormatUtils.format(c, "yyyy-MM-dd");
	}

	public static String getDelayDay(String now, Integer delay, String pattern) {
		return getDelay(now, delay, pattern, 24 * 60 * 60);
	}

	public static String getDelayHour(String now, Integer delay, String pattern) {
		return getDelay(now, delay, pattern, 60 * 60);
	}

	public static String getDelayMinute(String now, Integer delay,
										String pattern) {
		return getDelay(now, delay, pattern, 60);
	}

	public static Date parse(String date, String pattern) {
		SimpleDateFormat format = new SimpleDateFormat(pattern);
		ParsePosition pos = new ParsePosition(0);

		return format.parse(date, pos);
	}
	public static Date parse(String date) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		ParsePosition pos = new ParsePosition(0);
		return format.parse(date, pos);
	}


	public static String getWeek(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);

		return DateFormatUtils.format(c, "EEEE");
	}
	// 获取前count天
	public static String getAddCountDay(int count,String day){

		SimpleDateFormat simpleDate2 = new SimpleDateFormat("yyyy-MM-dd");
		// 指定日期/时间分析是否不严格 ,false为严格
		simpleDate2.setLenient(false);
		Date myDate;
		try {
			myDate = simpleDate2.parse(day);
			Long myTime=(myDate.getTime()/1000)+60*60*24*count;
			myDate.setTime(myTime*1000);
			// 返回String
			return  simpleDate2.format(myDate);
		} catch (ParseException e) {
			//e.printStackTrace();
		}

		return day;
	}
	/**
	 *  对比两个字符串格式的日期，返回相差日期
	 *  如果date1比date2大，返回负数。
	 * **/
	public static int contrastStringDate(String date1,String date2){

		int day=0;

		SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd");

		GregorianCalendar calendar1=new GregorianCalendar();
		GregorianCalendar calendar2=new GregorianCalendar();
		//String s1="2006-04-21";
		//String s2="2006-04-25";
		Date xxx1=new Date();
		Date xxx2=new Date();
		try {
			xxx1=sf.parse(date1);
			xxx2=sf.parse(date2);
			day = (int) ((xxx2.getTime() - xxx1.getTime()) / 3600 / 24 / 1000);
		} catch (ParseException e) {

			//e.printStackTrace();
		}

		return day;
	}

	/**
	 * 获取两个时间相差的分钟数
	 * @param start 开始时间
	 * @param end   结束时间
	 * @return      分钟数
	 */
	public static long hasMinute(Date start,Date end){
		try {
			System.out.println(start+"======="+end);
			long result=end.getTime()-start.getTime();
			System.out.println(end.getTime()+"========"+start.getTime());
			result = result / (1000 * 60);//分钟数
			return result;
		} catch (Exception e) {
			return 0;
		}
	}

	/**
	 * 获取某年某月的最后一天
	 * @param date 日期 形式yyyy-MM-dd
	 * @author guojt
	 * @return
	 */
	public static String getLastDayOfMonth(String date) {
		String[] ss = date.split("-");
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.YEAR, Integer.valueOf(ss[0]));
		cal.set(Calendar.MONTH, Integer.valueOf(ss[1])-1);
		int maxDay = cal.getActualMaximum(Calendar.DATE);
		return ss[0] + "-" + ss[1] + "-" + maxDay;
	}

	/**
	 * 根据年份、周返回指定月份的日期段
	 * @param year 年
	 * @param week 周
	 * @param month 月
	 * @return
	 */
	public static String getDateByWeek(int year,int week,int month){
		String ret = "";
		Calendar c = Calendar.getInstance();
		c.set(year, 0, 1);
		int day_week = c.get(Calendar.DAY_OF_WEEK);
		//c.add(Calendar.DATE, -(day_week-1));
		c.add(Calendar.DATE, 7*week-(day_week-1));

		if((month - c.get(Calendar.MONTH)) != 1){
			ret += "1-";
			c.add(Calendar.DATE, 6);
			ret += c.get(Calendar.DAY_OF_MONTH);
		}else{
			int sun_week = c.get(Calendar.DAY_OF_MONTH);
			ret += sun_week + "-";
			c.add(Calendar.DATE, 6);
			int sat_week = c.get(Calendar.DAY_OF_MONTH);
			if(sat_week > sun_week){
				ret += sat_week;
			}else{
				ret += (6 - sat_week + sun_week);
			}
		}

		return ret;
	}

	/**
	 * 月份相减
	 * @author guojt
	 * @param date1 开始日期 格式yyyy-MM
	 * @param date2 结束日期 格式yyyy-MM
	 * @return 两个日期相差的月数
	 */
	public static int getMonthInterval(String date1,String date2) {
		try{
			SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM");
			Calendar cal1=Calendar.getInstance();
			cal1.setTime(fmt.parse(date1));
			Calendar cal2=Calendar.getInstance();
			cal2.setTime(fmt.parse(date2));
			//年份之差 + 月份之差
			return (cal2.get(1)-cal1.get(1))*12+(cal2.get(2)-cal1.get(2));
		}catch (Exception e) {
			e.printStackTrace();
		}
		return 0;
	}

	/**
	 * 月份相加
	 * @author guojt
	 * @param date 起始日期 格式：yyyy-MM
	 * @param n 月数
	 * @return 起始日期加上n个月后的日期
	 */
	public static String getAddCountMonth(String date,int n){
		try{
			SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM");
			Calendar c = Calendar.getInstance();
			c.setTime(fmt.parse(date));
			c.add(Calendar.MONTH, n);
			return fmt.format(c.getTime());
		}catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 得到当前日期是周几，返回的是数字 0是周日--6是周六
	 * @param date
	 * @return
	 */
	public static int getThisDateOfWeek(String date){
		try {
			SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
			Calendar cd = Calendar.getInstance();
			cd.setTime(fmt.parse(date));
			// 获得今天是一周的第几天，星期日是第一天，星期一是第二天......
			int dayOfWeek = cd.get(Calendar.DAY_OF_WEEK) - 1;
			return dayOfWeek;
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return -1;
	}

	/**
	 * 获取某年某月的第一天
	 * @param date 日期 形式yyyy-MM-dd
	 * @return
	 */
	public static String getFirstDayOfMonth(String date){
		String[] ss = date.split("-");
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.YEAR, Integer.valueOf(ss[0]));
		cal.set(Calendar.MONTH, Integer.valueOf(ss[1])-1);
		int minDay = cal.getActualMinimum(Calendar.DATE);
		if(minDay < 10){
			return ss[0] + "-" + ss[1] + "-0" + minDay;
		}else{
			return ss[0] + "-" + ss[1] + "-" + minDay;
		}
	}

	/**
	 * 获取当年的第一天
	 * @param
	 * @return
	 */
	public static Date getCurrYearFirst(){
		Calendar currCal=Calendar.getInstance();
		int currentYear = currCal.get(Calendar.YEAR);
		return getYearFirst(currentYear);
	}


	/**
	 * 获取某年第一天日期
	 * @param year 年份
	 * @return Date
	 */
	public static Date getYearFirst(int year){
		Calendar calendar = Calendar.getInstance();
		calendar.clear();
		calendar.set(Calendar.YEAR, year);
		Date currYearFirst = calendar.getTime();
		return currYearFirst;
	}
	/**
	 * 获取某年最后一天日期
	 * @param year 年份
	 * @return Date
	 */
	public static Date getCurrYearLast(int year){
		Calendar calendar = Calendar.getInstance();
		calendar.clear();
		calendar.set(Calendar.YEAR, year);
		calendar.roll(Calendar.DAY_OF_YEAR, -1);
		Date currYearLast = calendar.getTime();

		return currYearLast;
	}
	/**
	 * 判断这个日期是否这个日期所在年的第一天
	 * @param date
	 * @return
	 */
	public static boolean judgeIsFirstDayOfYear(String date){
		Date firstDay =getYearFirst(Integer.parseInt(format(parse(date), "yyyy")));// getYearFirst();
		if(format(parse(date),"yyyy-MM-dd").equals(format(firstDay,"yyyy-MM-dd"))){
			return true;
		}else{
			return false;
		}
	}
	/**
	 * 判断这个日期是否这个日期所在年的最后一天
	 * @param date
	 * @return
	 */
	public static boolean judgeIsLastDayOfYear(String date){
		Date lastDay =getCurrYearLast(Integer.parseInt(format(parse(date), "yyyy")));// getYearFirst();
		if(format(parse(date),"yyyy-MM-dd").equals(format(lastDay,"yyyy-MM-dd"))){
			return true;
		}else{
			return false;
		}
	}
	public static void main(String[] args) {
		System.out.println(getFirstDayOfMonth(format(new Date())));
		judgeIsLastDayOfYear("2013-12-30");
	}

	/**
	 * 判断这个日期是否这个日期所在月的第一天
	 * @param date
	 * @return
	 */
	public static boolean judgeIsFirstDayOfMonth(String date){
		String firstDay = getFirstDayOfMonth(date);
		if(date.equals(firstDay)){
			return true;
		}else{
			return false;
		}
	}

	/**
	 * 判断这个日期是否这个日期所在月的最后一天
	 * @param date
	 * @return
	 */
	public static boolean judgeIsLastDayOfMonth(String date){
		String lastDay = getLastDayOfMonth(date);
		if(date.equals(lastDay)){
			return true;
		}else{
			return false;
		}
	}



/*
    public static void main(String[] agrs){
//      System.out.println(getDateByWeek(2011,9,3));
//      System.out.println(DateUtils.getMonthInterval("2010-05-01","2011-01-09"));
//      System.out.println(DateUtils.getAddCountMonth("2010-05",3));
//      System.out.println(DateUtils.format(new Date(), "yyyyMM"));
//      System.out.println(hasMinute((new Date(2010-1900,11-1,15,10,16,0)),(new Date())));
        String d="15-12月-11 06.10.36.000000 下午";
        Date date=parse(d, "dd-MM月-yy hh.mm.ss.SS a");
        System.out.println(date);
        System.out.println(format(date,"yyyy-MM-dd HH:mm:ss"));
        System.out.println(format(new Date(),"dd-MM月-yy hh.mm.ss.SS a"));
    }*/
}




